var FormData = require('form-data');
var config = require('./config');
var crypto = require('crypto');

var cfg = {
    ip : '10.12.16.222',
    port : '13520',
};

var args = process.argv.splice(2);
if (!args[0]) {
    console.log('args : gmType\nargs : gmType ip port');
    return;
}
if (args[1] && args[2]) {
    cfg.ip = args[1];
    cfg.port = args[2];
    console.log('ip :', cfg.ip, 'port :', cfg.port);
}

let gmFileName = './gm/gm' + args[0];
var gm = require(gmFileName);
if (!gm) {
    console.log("gmCmd args error");
    return;
}

let form = new FormData();
form.append("auth", aesEncrypt());
form.append("content", gm);

var request_url = 'http://' + cfg.ip + ':' + cfg.port;
console.log("request_url ==> ", request_url)
form.submit(request_url, function (err, res) {
});

function aesEncrypt() {
    let secret = config.secret;
    let data = Date.now().toString();
    const cipher = crypto.createCipher('aes256', secret);
    var crypted = cipher.update(data, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;
  }